package com.bootdo.oa.controller;

import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.SessionService;

import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.oa.domain.ClientMessage;
import com.bootdo.oa.domain.ServerMessage;

import java.security.Principal;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Controller
public class WebSocketController {
	@Autowired
	SimpMessagingTemplate template;

	@Autowired
    SessionService sessionService;
/*
	@Autowired
	WelcomeTask welcomeTask;

	@MessageMapping("/welcome") // 浏览器发送请求通过@messageMapping 映射/welcome 这个地址。
	@SendTo("/topic/getResponse") // 服务器端有消息时,会订阅@SendTo 中的路径的浏览器发送消息。
	public ServerMessage say(ClientMessage message) throws Exception {
		Thread.sleep(1000);
		return new ServerMessage("Welcome, " + message.getName() + "!");
	}*/

	@GetMapping("/test")
	String test() {
		return "test";
	}

	/*@RequestMapping("/welcome")
	@ResponseBody
	public R say02() {
		try {
			welcomeTask.sayWelcome();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return R.ok();
	}*/
//    @ResponseBody
//    @GetMapping("/chat")
//    public String  handleChat(Principal principal, String msg) {
//        template.convertAndSendToUser(sessionService.listPrincipal().get(0).toString(), "/queue/notifications", principal.getName() + "给您发来了消息：" + msg);
//        return sessionService.listPrincipal().get(0).toString();
//    }
	
    @ResponseBody
    @GetMapping("/chat")
    public String  handleChat() {
    	Long userId = ShiroUtils.getUserId();
    	if(userId.equals("1")){
    		UserDO user = ShiroUtils.getUser();
    		template.convertAndSendToUser(user.toString(), "/queue/notifications", "管理员" + "给您发来了消息：");
    	}
        return "1";
    }


    @MessageMapping("/sendTest")
    @SendTo("/topic/subscribeTest")
    public ServerMessage sendDemo(ClientMessage message) {
        
        System.out.println("接收到了信息" + message.getName());
        return new ServerMessage("Welcome123213213123, " + message.getName()+ "!");
    }

    @SubscribeMapping("/subscribeTest")
    public ServerMessage sub() {
       
        System.out.println("XXX用户订阅了我。。。");
        return new ServerMessage("感谢你订阅了我。。。");
    }

	
	
}